package chain_token

import (
	"chain_bridge_service/internal/pkg/core"
	chian_to_chain "chain_bridge_service/internal/repository/mongodb/chain_to_chain"
	"chain_bridge_service/internal/repository/mongodb/chain_token"
)

type RequestData struct {
	ChainConfigId string
	UserId        string
}

func (s *service) List(ctx core.Context, chainData *RequestData) (chainTokenList []chain_token.ChainToken, err error) {
	chainToChainModel := chian_to_chain.NewModel()
	chainList, err := chainToChainModel.ApiList(s.db.GetDb(), chainData.UserId)
	if err != nil {
		return nil, err
	}
	tokenIds := make(map[string]bool)
	for _, result := range chainList {
		tokenIds[result["token0"].(string)] = true
		tokenIds[result["token1"].(string)] = true
	}
	model := chain_token.NewModel()
	var list []chain_token.ChainToken
	for key, _ := range tokenIds {
		model.Id = key
		model.ChainConfigId = chainData.ChainConfigId
		tokenInfo := model.GetInfoById(s.db.GetDb())
		if tokenInfo.Id != "" {
			list = append(list, tokenInfo)
		}
	}
	return list, err
}

func (s *service) GetListByConfigId(ctx core.Context, chainData *RequestData) (chainTokenList []chain_token.ChainToken, err error) {

	model := chain_token.NewModel()
	model.ChainConfigId = chainData.ChainConfigId
	var list []chain_token.ChainToken
	list, err = model.List(s.db.GetDb())
	return list, err
}
